package gzhaoku.com.common.ui.components;

import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;

public class XWebView extends WebView {
    public XWebView(Context context) {
        super(context);
        initDefaultSetting();
    }

    public XWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initDefaultSetting();
    }

    public XWebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initDefaultSetting();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public XWebView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initDefaultSetting();
    }

    public XWebView(Context context, AttributeSet attrs, int defStyleAttr, boolean privateBrowsing) {
        super(context, attrs, defStyleAttr, privateBrowsing);
        initDefaultSetting();
    }



    public void initDefaultSetting(){
        WebSettings settings = this.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setPluginState(WebSettings.PluginState.ON);
        settings.setAllowFileAccess(true);
        settings.setAllowUniversalAccessFromFileURLs(true);
        settings.setLoadWithOverviewMode(true);
        settings.setUseWideViewPort(true);
        settings.setDomStorageEnabled(true);
        settings.setAppCacheEnabled(true);
        settings.setCacheMode(WebSettings.LOAD_DEFAULT);
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        settings.setMediaPlaybackRequiresUserGesture(false);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            settings.setSafeBrowsingEnabled(true);
        }

        this.setWebChromeClient(new WebChromeClient());
        this.setWebViewClient(new XWebViewClient());

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            WebView.setWebContentsDebuggingEnabled(true);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
//        mInsideWebChromeClient = new InsideWebChromeClient();
//        InsideWebViewClient mInsideWebViewClient = new InsideWebViewClient();
//        //javascriptInterface = new JavascriptInterface();
//        //mWebView.addJavascriptInterface(javascriptInterface, "java2js_laole918");
//        mWebView.setWebChromeClient(mInsideWebChromeClient);
//        mWebView.setWebViewClient(mInsideWebViewClient);
    }

}
